/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-24
*/
package com.rzico.order.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.VirtualStock;
import com.rzico.order.model.VirtualStockVo;
import com.rzico.order.service.VirtualStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 虚拟商品库存控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-07-24
 */
@Api(description = "虚拟商品库存接口")
@RestController
@RequestMapping("/member/virtualStock")
public class VirtualStockMemberController extends BaseController {

    @Autowired
    private VirtualStockService virtualStockService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询虚拟商品库存
     *
     * @return
    */
    @ApiOperation("分页查询虚拟商品库存")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "subType", value = "类型 {0.产品,1:套餐,2:水票,3.压桶}", dataType = "Long", paramType = "query")
    })
    public CommResult<VirtualStock> list(Long productId,Integer subType, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Member member = memberService.getCurrent();

        if (member!=null){
            params.put("memberId", member.getId());
        }
        if (productId!=null){
            params.put("productId", productId);
        }
        if (subType!=null){
            params.put("subType", subType);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<VirtualStockVo> list = virtualStockService.selectVoList(params);
        PageResult<VirtualStockVo> pageResult = new PageResult<VirtualStockVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }

}
